package com.mvc4g.test.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.client.Controller;
import com.mvc4g.client.Event;
import com.mvc4g.client.ViewInterface;
import com.mvc4g.test.client.Constants;

public class RootTemplate implements ViewInterface, Constants {

	private Controller controller = new Controller();

	private SimplePanel body = new SimplePanel();

	public RootTemplate(Panel rootParent, int nbViewsAndActions) {

		final ListBox pageNumber = new ListBox();
		for (int i = 0; i < nbViewsAndActions; i++) {
			pageNumber.addItem(Integer.toString(i));
		}

		Button choose = new Button("Choose");
		choose.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(CHANGE_PAGE
						+ pageNumber.getSelectedIndex(), null);
				controller.handleEvent(e);
			}

		});

		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.add(new Label("Choose a widget"));
		hPanel.add(pageNumber);
		hPanel.add(choose);

		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add(hPanel);
		mainPanel.add(body);

		rootParent.add(mainPanel);

	}
	
	public void changePage(Widget newView) {
		body.clear();
		newView.setVisible(true);		
		body.add(newView);
	}

	@Override
	public void setController(Controller controller) {
		this.controller = controller;
	}

}
